package com.ljkj.blockpay.ui.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import com.bumptech.glide.Glide;
import com.kongzue.baseokhttp.util.JsonMap;
import com.ljkj.blockpay.R;
import com.ljkj.blockpay.basedata.BaseActivity;
import com.ljkj.blockpay.databinding.ActivityWithdrawDetailBinding;
import com.ljkj.blockpay.model.RechargeModel;
import com.ljkj.blockpay.presenter.WithdrawDetailPresenter;
import com.ljkj.blockpay.presenter.inter.WithdrawInter;
import com.ljkj.blockpay.ui.adapter.RechargeAdapter;
import com.ljkj.blockpay.util.ToggleToActivity;
import com.ljkj.blockpay.widget.ClickListener;
import com.ljkj.blockpay.widget.CommonDialog;
import com.luck.picture.lib.tools.ToastUtils;

import java.util.List;

/**
 * 添加提币地址
 */
public class WithdrawDetailActivity extends BaseActivity<ActivityWithdrawDetailBinding,WithdrawDetailPresenter> implements WithdrawInter.WithdrawInterView, ClickListener {

    private String currencyName;
    private String currencyImg;
    private RecyclerView currencylist;
    private CommonDialog mSelectdialog;
    private RechargeAdapter mRechargeAdapter;
    private List<RechargeModel.NetworkDTO> modelList;
    private String networkTypes;
    @Override
    public int initContentView() {
        return R.layout.activity_withdraw_detail;
    }

    @Override
    protected WithdrawDetailPresenter createPresenter() {
        return new WithdrawDetailPresenter(this,this);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        binding.setOnClick(this::onClick);
        binding.tvTitle.setLeftBackFinish(this);
        binding.tvTitle.setTitleContent(R.string.mentionmoneyaddress);
        getPresenter().getNetAccuracy();

    }

    @Override
    protected void initData() {
        super.initData();
        mSelectdialog = CommonDialog.Builder.with(this).setContentView(R.layout.select_net)
                .setOnClickListener(R.id.tv_close, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mSelectdialog.dismiss();
                    }
                }).setCancelable(true).fullWidth().formBottom(true).create();

        GridLayoutManager lManager = new GridLayoutManager(this, 2);
        currencylist = mSelectdialog.findViewById(R.id.rv_currency);
        currencylist.setLayoutManager(lManager);
        mRechargeAdapter = new RechargeAdapter(R.layout.currnet_layout);
        currencylist.setAdapter(mRechargeAdapter);

        mRechargeAdapter.setOnItemClickListener((adapter, view, position) -> {
            modelList = mRechargeAdapter.getData();
            networkTypes = modelList.get(position).getNetworkType();

            binding.tvNet.setText(networkTypes);
            mSelectdialog.dismiss();
        });

        binding.etAddressalisa.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!binding.etAddress.getText().toString().equals("")&& !editable.toString().equals("")){
                    binding.btnSave.setBackground(getResources().getDrawable(R.drawable.btnbg));
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rl_select:
                ToggleToActivity.toSelectCurrencyActivity(this, 10);
                break;
            case R.id.rl_netselect:
                mSelectdialog.show();
                break;
            case R.id.et_address:

                break;
            case R.id.et_addressalisa:

                break;
            case R.id.btn_save:
                String address = binding.etAddress.getText().toString();
                String addressalisa = binding.etAddressalisa.getText().toString();
                if (address.equals("")){
                    ToastUtils.s(this,"提币地址不能为空");
                } else if (addressalisa.equals("")){
                    ToastUtils.s(this,"提币地址别名为空");
                } else {
                    getPresenter().addAddress();
                }

                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null){
            Bundle bundle = data.getExtras();
            JsonMap jsonMap = JsonMap.parse(bundle.getString("jsonMap"));
            switch (requestCode) {
                case 10:
                    currencyName = jsonMap.getString("currencyName");
                    currencyImg = jsonMap.getString("currencyImg");
                    binding.tvCurrency.setText(currencyName);
                    Glide.with(this).load(currencyImg).into(binding.ivCurrencyicon);
                    break;
                default:
                    break;



            }
        }
    }

    @Override
    public void showNetAccuracy(List<RechargeModel> modelList) {
        if (modelList != null){
            mRechargeAdapter.setNewInstance(modelList.get(0).getNetwork());
            List<RechargeModel.NetworkDTO> network = modelList.get(0).getNetwork();
            for (int i = 0; i < network.size(); i++) {
                binding.tvNet.setText(network.get(0).getNetworkType());
            }
            currencyName = modelList.get(0).getCurrencyName();
            currencyImg = modelList.get(0).getCurrencyImg();
            binding.tvCurrency.setText(currencyName);

            Glide.with(this).load(currencyImg).into(binding.ivCurrencyicon);
        }
    }

    //添加地址
    @Override
    public void addAddressSuccess(JsonMap jsonMap) {
        ToggleToActivity.toMMAddressActivity(this);
        finish();
    }

    //修改地址
    @Override
    public void updateAddressSuccess(JsonMap jsonMap) {
        ToggleToActivity.toMMAddressActivity(this);
    }
}